package com.lagou.sqlsession;

import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;

public class DefaultSqlSession implements SqlSession {

    private  Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStement().get(statementId);
        List<Object> objectList = new SimpleExecutor().query(configuration, mappedStatement, params);

        return (List<E>) objectList;
    }

    @Override
    public int insert(String statementId, Object... params) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStement().get(statementId);
        int insert = new SimpleExecutor().insert(configuration, mappedStatement, params);
        return insert;
    }

    @Override
    public int delete(String statementId, Object... params) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStement().get(statementId);
        int delete = new SimpleExecutor().deleteUser(configuration, mappedStatement, params);
        return delete;
    }

    @Override
    public int updateUser(String statementId, Object... params) throws Exception {
        MappedStatement mappedStatement = configuration.getMappedStement().get(statementId);
        int insert = new SimpleExecutor().update(configuration, mappedStatement, params);
        return insert;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> objects = selectList(statementId, params);
        if(objects.size()==1){
            return (T) objects.get(0);
        }else{
            throw new RuntimeException("查询结果为空或者返回的结果过多");
        }

    }

    @Override
    public <T> T  getMapper(Class<?> mapperClass) {
        // 使用jdk动态代理来为dao生成代理对象并返回  代理对象调用接口中的任一方法都会执行处理器中的invoke方法
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, (proxy, method, args) -> {
             // 底层还是去调用JDBC代码  //根据不同的情况，来调用不同的方法
            // 准备参数 1:stamentid: sql语句的唯一标识： namespace.id = 接口全限定名.方法名
            // 拿到方法名
            String methodName = method.getName();
            String className = method.getDeclaringClass().getName();
            String statementId =className+"."+methodName;

            // 准备参数2:params:args
            // 获取被调用的返回值类型
            Type genericReturnType = method.getGenericReturnType();
            // 判断是否进行了 泛型类型的参数化
            if(genericReturnType instanceof ParameterizedType){

                List<Object> objects = selectList(statementId, args);
                return objects;
            }
            if("insert".equals(methodName)){
                int i = insert(statementId, args);
                return i;
            }
            if("updateUser".equals(methodName)){
                int i = updateUser(statementId, args);
                return i;
            }
            if("deleteUser".equals(methodName)){
                int delete = delete(statementId, args);
                return delete;
            }



            return selectOne(statementId,args);
        });
        return (T) proxyInstance;
    }
}
